﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace HRMService
{
    public partial class MainService : ILeaveWork
    {
        public DataSet LeaveWork_GetAll()
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LeaveWork_GetAll", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public DataSet LeaveWork_GetMarkWork(int Month, int Year, int BranchID, int _DepartmentID)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LeaveWork_GetMarkWork", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Month", Month);
            dbCmd.Parameters.AddWithValue("@Year", Year);
            dbCmd.Parameters.AddWithValue("@BranchID", BranchID);
            dbCmd.Parameters.AddWithValue("@DepartmentID", _DepartmentID);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public DataSet LeaveWork_GetTotalMarkWork(int Month, int Year, int BranchID, int _DepartmentID)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LeaveWork_GetTotalMarkWork", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Month", Month);
            dbCmd.Parameters.AddWithValue("@Year", Year);
            dbCmd.Parameters.AddWithValue("@BranchID", BranchID);
            dbCmd.Parameters.AddWithValue("@DepartmentID", _DepartmentID);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public int LeaveWork_CountDay(int EmployeeID, int LeaveID)
        {
            int retval = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LeaveWork_CountDay", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@EmployeeID", EmployeeID);
            dbCmd.Parameters.AddWithValue("@LeaveID", LeaveID);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteScalar() != DBNull.Value)
                    retval = Convert.ToInt32(dbCmd.ExecuteScalar());
                else
                    retval = 0;
            }
            finally
            {
                dbConn.Close();
            }
            return retval;
        }

        public bool LeaveWork_CheckExistEmployee(int EmployeeID, int month, int year)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LeaveWork_CheckEmployee", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@LW_EmployeeID", EmployeeID);
            dbCmd.Parameters.AddWithValue("@month", month);
            dbCmd.Parameters.AddWithValue("@year", year);
            try
            {
                dbConn.Open();
                int Count = Convert.ToInt32(dbCmd.ExecuteScalar());
                retVal = Count > 0 ? true : false;
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet LeaveWork_GetList(out int ItemCount, int currentPage, int pageSize, int year, int BranchID, int DepartmentID, string keyWord)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LeaveWork_GetList", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@PageSize", pageSize);
            dbCmd.Parameters.AddWithValue("@keyWord", keyWord);
            dbCmd.Parameters.AddWithValue("@year", year);
            dbCmd.Parameters.AddWithValue("@BranchID", BranchID);
            dbCmd.Parameters.AddWithValue("@DepartmentID", DepartmentID);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet LeaveWork_GetWithEmployee(int EmployeeID, DateTime _date)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LeaveWork_GetWithEmployee", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@LW_EmployeeID", EmployeeID);
            dbCmd.Parameters.AddWithValue("@Date", _date);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);

            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet LeaveWork_GetDayUsed(int EmployeeID, int LeaveID)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LeaveWork_GetDayUsed", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@EmployeeID", EmployeeID);
            dbCmd.Parameters.AddWithValue("@LeaveID", LeaveID);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);

            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet LeaveWork_GetListWithEmployee(int EmployeeID, int _year)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LeaveWork_GetListWithEmployee", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@EmployeeID", EmployeeID);
            dbCmd.Parameters.AddWithValue("@year", _year);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);

            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet LeaveWork_Report(int BranchID, int DepartmentID, int month, int year)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LeaveWork_Report", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@BranchID", BranchID);
            dbCmd.Parameters.AddWithValue("@DepartmentID", DepartmentID);
            dbCmd.Parameters.AddWithValue("@month", month);
            dbCmd.Parameters.AddWithValue("@year", year);

            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);

            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet LeaveWork_GetAllWithDepartment(int DepartmentID)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LeaveWork_GetAllWithDepartment", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@DepartmentID", DepartmentID);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);

            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public LeaveWorkInfo LeaveWork_GetInfo(int LWID)
        {
            LeaveWorkInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LeaveWork_GetInfo", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@LW_ID", LWID);
            SqlDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new LeaveWorkInfo();
                    retVal.ID = Convert.ToInt32(dr["LW_ID"]);
                    retVal.EmployeeID = Convert.ToInt32(dr["LW_EmployeeID"]);
                    retVal.BranchID = Convert.ToInt32(dr["LW_BranchID"]);
                    retVal.DepartmentID = Convert.ToInt32(dr["LW_DepartmentID"]);
                    retVal.LeaveID = Convert.ToInt32(dr["LW_LeaveID"]);
                    retVal.FromDate = Convert.ToDateTime(dr["LW_FromDate"]);
                    retVal.ToDate = Convert.ToDateTime(dr["LW_ToDate"]);
                    retVal.Reason = dr["LW_Reason"].ToString();
                    retVal.TotalDays = Convert.ToDecimal(dr["LW_TotalDays"]);                   
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public LeaveWorkInfo LeaveWork_GetInfoEmployee(int EmployeeID)
        {
            LeaveWorkInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LeaveWork_GetInfoEmployee", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@EmployeeID", EmployeeID);
            SqlDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new LeaveWorkInfo();
                    retVal.ID = Convert.ToInt32(dr["LW_ID"]);
                    retVal.EmployeeID = Convert.ToInt32(dr["LW_EmployeeID"]);
                    retVal.BranchID = Convert.ToInt32(dr["LW_BranchID"]);
                    retVal.DepartmentID = Convert.ToInt32(dr["LW_DepartmentID"]);
                    retVal.LeaveID = Convert.ToInt32(dr["LW_LeaveID"]);
                    retVal.FromDate = Convert.ToDateTime(dr["LW_FromDate"]);
                    retVal.ToDate = Convert.ToDateTime(dr["LW_ToDate"]);
                    retVal.Reason = dr["LW_Reason"].ToString();
                    retVal.TotalDays = Convert.ToInt32(dr["LW_TotalDays"]);
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public int LeaveWork_Insert(LeaveWorkInfo eInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LeaveWork_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@LW_EmployeeID", eInfo.EmployeeID);
            dbCmd.Parameters.AddWithValue("@LW_BranchID", eInfo.BranchID);
            dbCmd.Parameters.AddWithValue("@LW_DepartmentID", eInfo.DepartmentID);
            dbCmd.Parameters.AddWithValue("@LW_LeaveID", eInfo.LeaveID);
            dbCmd.Parameters.AddWithValue("@LW_FromDate", eInfo.FromDate);
            dbCmd.Parameters.AddWithValue("@LW_ToDate", eInfo.ToDate);
            dbCmd.Parameters.AddWithValue("@LW_Reason", eInfo.Reason);
            dbCmd.Parameters.AddWithValue("@LW_TotalDays", eInfo.TotalDays);
            
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool LeaveWork_Update(LeaveWorkInfo eInfo)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LeaveWork_Update", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@LW_ID", eInfo.ID);
            dbCmd.Parameters.AddWithValue("@LW_EmployeeID", eInfo.EmployeeID);
            dbCmd.Parameters.AddWithValue("@LW_BranchID", eInfo.BranchID);
            dbCmd.Parameters.AddWithValue("@LW_DepartmentID", eInfo.DepartmentID);
            dbCmd.Parameters.AddWithValue("@LW_LeaveID", eInfo.LeaveID);
            dbCmd.Parameters.AddWithValue("@LW_FromDate", eInfo.FromDate);
            dbCmd.Parameters.AddWithValue("@LW_ToDate", eInfo.ToDate);
            dbCmd.Parameters.AddWithValue("@LW_Reason", eInfo.Reason);
            dbCmd.Parameters.AddWithValue("@LW_TotalDays", eInfo.TotalDays);
            
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public bool LeaveWork_Delete(int LWID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LeaveWork_Delete", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@LW_ID", LWID);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
    }
}

